package com.qf.dao.impl;


import com.qf.dao.OrderDao;
import com.qf.pojo.TbOrder;
import com.qf.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Create by 周志豪 on 2022/8/20  15:29
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */
public class OrderDaoImpl implements OrderDao {

    @Override
    public void addOrder(String orderId, Long userId, Long addressId, Long money) {
        QueryRunner runner = DbUtils.getRunner();

        String sql = "insert into tb_order (id,uid,money,aid) VALUE(?,?,?,?)";
        try {
            int update = runner.update(sql, orderId, userId, money, addressId);
        } catch (SQLException e) {
           e.printStackTrace();
        }
    }

    @Override
    public void addOrderDetail(String orderId, Long goodKzId, long num, long money) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "insert into tb_orderdetail  values (null,?,?,?,?)";
        try {
            int update = runner.update(sql, orderId, goodKzId, num, money);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int updateOrderStaus(String oid, String status) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "update tb_order set status=? where id =?";
        try {
            int update = runner.update(sql, status, oid);
            return update;
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return 0;
    }

    @Override
    public TbOrder findById(String orderId) {
        String sql = "SELECT * FROM tb_order WHERE id=?";
        QueryRunner runner = DbUtils.getRunner();
        TbOrder order = null;
        try {
            order = runner.query(sql, new BeanHandler<>(TbOrder.class), orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public List<TbOrder> findByUserId(Long userId) {
        String sql = "SELECT * FROM tb_order WHERE uid=?";
        QueryRunner runner = DbUtils.getRunner();
        List<TbOrder> orders = null;
        try {
            orders = runner.query(sql, new BeanListHandler<>(TbOrder.class), userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }


}
